package com.jackstraw.jack_base.util;

import cn.hutool.core.util.StrUtil;
import org.apache.commons.lang3.StringUtils;

/**
 * 字符串工具类
 */
public class StringUtil {


    /**
     * 字符串判空
     *
     * @param origin
     * @return
     */
    public static String null2Str(String origin) {
        return null2Str(origin, "");
    }

    /**
     * 字符串判空
     *
     * @param origin
     * @param def
     * @return
     */
    public static String null2Str(String origin, String def) {
        return ((origin == null) ? def : origin.toString().trim());
    }

    /**
     * 下划线转驼峰
     *
     * @param str
     * @return
     */
    public static String toCamelCase(String str) {
        return StringUtilsByHutool.toCamelCase(str);
    }

    /**
     * 驼峰转下划线
     *
     * @param str
     * @return
     */
    public static String toUnderlineCase(String str) {
        return StringUtilsByHutool.toUnderlineCase(str);
    }

    /**
     * 驼峰转指定连接符
     *
     * @param str
     * @return
     */
    public static String toSymbolCase(String str, char symbol) {
        return StringUtilsByHutool.toSymbolCase(str, symbol);
    }

    /**
     * 将字符串的首字母转大写
     *
     * @param str 需要转换的字符串
     * @return
     */
    public static String captureName(String str) {
        // 进行字母的ascii编码前移，效率要高于截取字符串进行转换的操作
        char[] cs = str.toCharArray();
        cs[0] -= 32;
        return String.valueOf(cs);
    }


    /**
     * 占位符替换
     *
     * @param str
     * @param obj
     * @return
     */
    public static String placeholderAnalysis(String str, Object... obj) {
        return String.format(str.replaceAll("\\{[^}]*\\}", "%s"), obj);
    }

    /**
     * 将占位符 %s 替换
     *
     * @param str
     * @param obj
     * @return
     */
    public static String format(String str, Object... obj) {
        return String.format(str, obj);
    }

    /**
     * 将类名称转换为beanName
     *
     * @param className
     * @return
     */
    public static String beanName(String className) {
        String[] path = className.split("\\.");
        String beanName = path[path.length - 1];
        return Character.toLowerCase(beanName.charAt(0)) + beanName.substring(1);
    }

    public static class StringUtilsBylang3 extends StringUtils {
    }

    public static class StringUtilsByHutool extends StrUtil {
    }

    public static class StringUtilsBySpring extends org.springframework.util.StringUtils {
    }
}
